package com.jinzecloud.fiction.aop;

import cn.hutool.json.JSONUtil;
import com.jinzecloud.fiction.annotation.TestAnnotation;
import com.jinzecloud.fiction.service.TbAuthorService;
import lombok.RequiredArgsConstructor;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.Arrays;

@Aspect
@Component
@RequiredArgsConstructor
public class TestAop {

    private final TbAuthorService authorService;

    @Pointcut(value = "@annotation(com.jinzecloud.fiction.annotation.TestAnnotation)")
    public void exec() {

    }

    @Around("exec()")
    public Object after(ProceedingJoinPoint point) {
        StringBuilder sb = new StringBuilder();
        if (point.getSignature() instanceof MethodSignature signature) {
            TestAnnotation annotation = signature.getMethod().getAnnotation(TestAnnotation.class);
            if (annotation != null) {
                System.out.println(annotation.value());
            }
            System.out.println("参数：" + Arrays.toString(point.getArgs()));
        }
        Object proceed = null;
        System.out.println("开启事务");
        try {
            proceed = point.proceed();
        } catch (Throwable e) {
            System.out.println("回滚事务");
            throw new RuntimeException(e);
        }
        System.out.println("提交事务");
        System.out.println("返回值" + JSONUtil.toJsonStr(proceed));
        System.out.println("TestAop");
        System.out.println();
        return proceed;
    }

}
